<?php
class App_Module_Admin_Component_Structure_Ctrl_Http_Ajax extends App_Module_Admin_Component_Structure_Ctrl_Http
{
	public function subtreePrAction()
	{
		$data = $this->GetTpl('Web')->SubCatList($this->_cid, $this->_subCat, $this->GetExpandedElements());
		Mozg::Response('http')->SetBody($data);
	}

    public function updatesortPrAction ()
    {
        $catIds = (array)Mozg::Request('http')->getPost('scat');
        $result = Mozg_Mapper::Get('Structure')->Sort($catIds);
		if (!$result)
		{
			Mozg::Lib('messanger')->createMessage(Mozg_Lang::_('ERROR_OCCURED'), MESSAGE_ERROR);
	        Mozg::Response('http')->SetStatus(Mozg_Response::StatusError);
		}
		else
		{
			Mozg::Lib('messanger')->createMessage(Mozg_Lang::_('SORT_SAVED'));
		}
    }

    public function movePrAction ()
    {
        $catId = intval(Mozg::Request('http')->getQuery('drag'));
        $parentId = intval(Mozg::Request('http')->getQuery('drop'));

        $item = Mozg_Mapper::Get('Structure')->GetOne(array('id' => $catId));

        if (!$item)
        {
			Mozg::Lib('messanger')->createMessage(Mozg_Lang::_('CATEGORY_NOT_FOUND'), MESSAGE_ERROR);
	        Mozg::Response('http')->SetStatus(Mozg_Response::StatusError);
        }
        else
        {
        	$item->SetParentId($parentId);
	        $result = Mozg_Mapper::Get('Structure')->Save($item);
	        if ($result)
	        {
	            Mozg::Lib('messanger')->createMessage(Mozg_Lang::_('CATEGORY_MOVED'));
	        }
	        else
	        {
	        	if (!$item->Validator()->GetState())
	        	{
	        		Mozg::Lib('messanger')->createMessage(Mozg_Lang::_('INVALID_DATA'), MESSAGE_ERROR);
	        	}
	        	else
	        	{
		            Mozg::Lib('messanger')->createMessage(Mozg_Lang::_('ERROR_OCCURED'), MESSAGE_ERROR);
	        	}

		        Mozg::Response('http')->SetStatus(Mozg_Response::StatusError);
	        }
        }
    }

    public function mediaaddPrAction()
    {
        $baseUrlParams = array(
        						'module' => 'admin',
        						'ctrl' => $this->GetName(),
        						'action' => 'mediaadd',
        						'cat' => $this->_cid,
        						'subcat' => $this->_subCat
        					);

		$data = $this->GetCtrl('media')->OutMediaFilesBlock($baseUrlParams, array('contentButton' => true));
        Mozg::Response('http')->SetBody($data);
    }

    public function get_module_params_blockPrAction()
    {
      	if ($this->_subCat)
      	{
      		$category = Mozg_Mapper::Get('Structure')->GetOne(array('id' => $this->_subCat));
      	}
      	else
      	{
      		$category = Mozg_Mapper::Get('Structure')->GetEntity();
      	}

      	$pageType = intval(Mozg::Request('http')->get('page_type'));
      	$category->SetPageType($pageType);

      	$t = $this->GetTpl('Moduleparams');
      	$data = $t->GetBlock($category->GetModuleParams());

      	Mozg::Response('http')->SetBody($data);
    }

    protected function NewField($tpl)
    {
    	$groupName = Mozg::Request('http')->get('groupname');
		$fieldName = Mozg::Request('http')->get('fieldname');
		$fieldLang = Mozg::Request('http')->get('fieldlang');
		$fieldParamsPath = Mozg::Request('http')->get('fieldparamspath');

        $obj = Mozg_Entity::Get($fieldParamsPath);

        $response = new stdClass();
        $response->value = $this->GetTpl($tpl)->GetFieldInput($groupName, $fieldName, $obj, $fieldLang);
        $response->field = $fieldName;

        Mozg::Response('http')->SetBody($response);
    }

    public function newpagefieldPrAction()
    {
    	$this->NewField('Pageparams');
    }

    public function newmodulefieldPrAction()
    {
    	$this->NewField('Moduleparams');
    }
}